package com.googlecode.mailnet.emf.netmodel.ui;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.gmf.runtime.diagram.ui.editparts.DiagramEditPart;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.handlers.HandlerUtil;

import com.googlecode.mailnet.emf.netmodel.NetworkConfigurationModel;
import com.googlecode.mailnet.emf.netmodel.diagram.edit.parts.NetworkConfigurationModelEditPart;
import com.googlecode.mailnet.emf.netmodel.diagram.part.NetmodelDiagramEditor;
import com.googlecode.mailnet.emf.netmodel.simulation.SimulationContext;


public class SimulationNextHandler extends AbstractHandler {

	@Override
	public Object execute(ExecutionEvent event) throws ExecutionException {
		IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindowChecked(event);
		
		IEditorPart editorPart = window.getActivePage().getActiveEditor();
		if (! (editorPart instanceof NetmodelDiagramEditor)) return null;
		NetmodelDiagramEditor editor = (NetmodelDiagramEditor) editorPart;
		DiagramEditPart diagramEditPart = editor.getDiagramEditPart();
		NetworkConfigurationModelEditPart modelEditPart = (NetworkConfigurationModelEditPart) diagramEditPart;
		
		NetworkConfigurationModel model = (NetworkConfigurationModel) modelEditPart.resolveSemanticElement();
		SimulationContext context = model.getSimulationContext();
		
		if (context != null) {
			context.next();
			modelEditPart.getFigure().repaint();
		}
		
		return null;
	}
	
//	public boolean isEnabled() {
//		IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
//		
//		IEditorPart editorPart = window.getActivePage().getActiveEditor();
//		if (! (editorPart instanceof NetmodelDiagramEditor)) return false;
//		NetmodelDiagramEditor editor = (NetmodelDiagramEditor) editorPart;
//		DiagramEditPart diagramEditPart = editor.getDiagramEditPart();
//		NetworkConfigurationModelEditPart modelEditPart = (NetworkConfigurationModelEditPart) diagramEditPart;
//		
//		SimulationContext context = modelEditPart.getSimulationContext();
//		return context != null;
//	}
}
